/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.dialogs;

import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.toedo.controllers.DataController;
import org.toedo.gui.translations.Messages;
import org.toedo.model.BaseNote;
import org.toedo.model.Note;
import org.toedo.model.Task;

public class InsertBaseNoteLinkDialog extends Dialog {

	private int mMode;
	
	private Combo mBaseNoteCombo;
	private Text mTitleText;
	
	private String mTitle = null;
	private UUID mId;
	
	public InsertBaseNoteLinkDialog(Shell parentShell, int mode) {
		super(parentShell);
		mMode = mode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		
		return contents;
	}
	
	protected Control createDialogArea(Composite parent) {
		
		Composite mainPanel = (Composite) super.createDialogArea(parent);
		mainPanel.setLayout(new GridLayout(1, false));
		GridData mainGD = new GridData(SWT.FILL, SWT.FILL, true, true);
		mainGD.widthHint = 350;
		mainPanel.setLayoutData(mainGD);
		
		Composite composite = new Composite(mainPanel, SWT.NONE);		
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Label linkBaseNoteLabel = new Label(composite, SWT.NONE);
		linkBaseNoteLabel.setText(Messages.InsertBaseNoteLinkDialog_LinkTo);
		
		mBaseNoteCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		mBaseNoteCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		fillCombo();
		
		Label linkTitleLabel = new Label(composite, SWT.NONE);
		linkTitleLabel.setText(Messages.InsertBaseNoteLinkDialog_LinkTitle);
		
		mTitleText = new Text(composite, SWT.BORDER);
		mTitleText.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
	
		return parent;
	}
	
	private void fillTasksCombo() {
		List<Task> taksList = DataController.getInstance().getProject().getTasksList();
		
		int index = 0;
		Task task;
		Iterator<Task> iter = taksList.iterator();
		while (iter.hasNext()) {
			task = iter.next();
			mBaseNoteCombo.add(task.getTitle());
			mBaseNoteCombo.setData(Integer.toString(index), task);
			index++;
		}
	}
	
	private void fillNotesCombo() {
		List<Note> noteList = DataController.getInstance().getProject().getNotesList();
		
		int index = 0;
		Note note;
		Iterator<Note> iter = noteList.iterator();
		while (iter.hasNext()) {
			note = iter.next();
			mBaseNoteCombo.add(note.getTitle());
			mBaseNoteCombo.setData(Integer.toString(index), note);
			index++;
		}
	}
	
	private void fillCombo() {		
		switch (mMode) {
		case 0 : fillTasksCombo(); break;
		case 1 : fillNotesCombo(); break;
		default : fillTasksCombo(); break;
		}
		mBaseNoteCombo.setText(mBaseNoteCombo.getItem(0));
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		Button okBtn = createButton(parent, -1, IDialogConstants.OK_LABEL, true);
		Button closeBtn = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		
		okBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {								 
				mTitle = mTitleText.getText();
				mId = ((BaseNote) mBaseNoteCombo.getData(Integer.toString(mBaseNoteCombo.getSelectionIndex()))).getId();
				close();
			}
		});
		
		closeBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {				
				close();				
			}
		});
	}
	
	public String getTitle() {
		return mTitle; 
	}
	
	public UUID getId() {
		return mId; 
	}

}
